package com.tanhua.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.AnalysisByDay;
import com.tanhua.domain.Log;
import com.tanhua.domain.SummaryVo;
import com.tanhua.mapper.AnalysisByDayMapper;
import com.tanhua.mapper.LogMapper;
import org.apache.commons.validator.routines.checkdigit.ABANumberCheckDigit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class SummaryService {
    @Autowired
    private LogMapper logMapper;
    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;
    //查询概要统计信息
    public ResponseEntity findSummary() throws ParseException {
         //累计用户  注册用户
          QueryWrapper<AnalysisByDay> qw=new QueryWrapper<AnalysisByDay>();
        List<AnalysisByDay> list=analysisByDayMapper.selectList(qw);

        Integer cumulativeUsers=0;
        if(list!=null){
            for (AnalysisByDay analysisByDay : list) {
                cumulativeUsers=cumulativeUsers+analysisByDay.getNumRegistered();
            }
        }

        Date date=DateUtil.date();

        Date yesterday =DateUtil.yesterday();

        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH,-1);
         Date time = c.getTime();
          QueryWrapper<AnalysisByDay> qw1=new QueryWrapper<>();
           qw1.between("record_date",time,date);
        List<AnalysisByDay> list1=analysisByDayMapper.selectList(qw1);
           Integer activePassMonth=0;
        if(list!=null){
            for (AnalysisByDay analysisByDay : list1) {
                activePassMonth=activePassMonth+analysisByDay.getNumActive();
            }
        }

        //过去7天活跃用户
        Calendar c2 = Calendar.getInstance();
        c2.add(Calendar.DATE,-7);
        Date time1=c2.getTime();
        QueryWrapper<AnalysisByDay> qw2=new QueryWrapper<>();
        qw2.between("record_date",time1,date);
        List<AnalysisByDay> list2=analysisByDayMapper.selectList(qw2);
        Integer activePassWeek=0;
        if(list!=null){
            for (AnalysisByDay analysisByDay : list2) {
                activePassWeek=activePassMonth+analysisByDay.getNumActive();
            }
        }

        //今日新增用户
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
        String format1=sdf.format(yesterday);
        Date yest=sdf.parse(format1);
        Date toda = sdf.parse(format);

        QueryWrapper<AnalysisByDay> qw3=new QueryWrapper<>();
        qw3.eq("record_date",toda);
        AnalysisByDay analysisByDay=analysisByDayMapper.selectOne(qw3);
        Integer newUsersToday=analysisByDay.getNumRegistered();


        QueryWrapper<AnalysisByDay> qw4=new QueryWrapper<>();
        qw4.eq("record_date",yest);
        AnalysisByDay analysisByDayyest=analysisByDayMapper.selectOne(qw4);
        Integer yestToday=analysisByDayyest.getNumRegistered();

        //今日新增用户涨跌率，单位百分数，正数为涨，负数为跌
        Integer newUsersTodayRate=0;
           if(yestToday!=0){
               newUsersTodayRate=(int)(((newUsersToday-yestToday)*1.0/yestToday)*100);
    }

        //今日登录次数
         Integer loginTimesToday=analysisByDay.getNumLogin();
         //今日登录次数涨跌率，单位百分数，正数为涨，负数为跌
        Integer yest1=analysisByDayyest.getNumLogin();
        Integer loginTimesTodayRate=0;
        if(yest1!=0){
            loginTimesTodayRate= (int)(((loginTimesToday-yest1)*1.0/yest1)*100);
        }

        //今日活跃用户
           Integer activeUsersToday=analysisByDay.getNumActive();
        //今日活跃用户涨跌率，单位百分数，正数为涨，负数为跌
          Integer yest2=analysisByDayyest.getNumActive();
        Integer activeUsersTodayRate=0;
          if(yest2!=0){

              activeUsersTodayRate=(int)(((activeUsersToday-yest2)*1.0/yest2)*100);
          }



        SummaryVo summaryVo= new SummaryVo();
        summaryVo.setCumulativeUsers(cumulativeUsers);
        summaryVo.setActivePassMonth(activePassMonth);
        summaryVo.setActivePassWeek(activePassWeek);
        summaryVo.setNewUsersTodayRate(newUsersTodayRate);
        summaryVo.setNewUsersToday(newUsersToday);
        summaryVo.setLoginTimesToday(loginTimesToday);
        summaryVo.setLoginTimesTodayRate(loginTimesTodayRate);
        summaryVo.setActiveUsersToday(activeUsersToday);
        summaryVo.setActiveUsersTodayRate(activeUsersTodayRate);
          return ResponseEntity.ok(summaryVo);
    }
}
